package com.everyline.baselibrary.view;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.Layout;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatTextView;

/**
 * Created by on 2020/10/22.
 * 天天挂线
 * 746608431@qq.com
 * 描述:TODO
 */

public class StrikethroughTextview extends AppCompatTextView {

    private float xAxisLeft, xAxisRight;
    private int start, end, colorResId = 0xffe64243;
    private Paint mPaint;
    private boolean isSHowStrikethrough = false;

    public StrikethroughTextview(Context context) {
        super(context);
        init();
    }

    public StrikethroughTextview(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public StrikethroughTextview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        // 设置直线没有锯齿
        mPaint.setAntiAlias(true);
        // 设置线宽
        mPaint.setStrokeWidth((float)3.0);
    }

    public void setStratAndEnd(int start, int end, int colorResId, boolean isSHowStrikethrough) {
        this.start = start;
        this.end = end;
        this.colorResId = colorResId;
        this.isSHowStrikethrough = isSHowStrikethrough;
    }

    private void getAxis() {
        if (null != mPaint)
            // 设置直线的颜色
            mPaint.setColor(colorResId);
        Layout layout = getLayout();
        xAxisLeft = layout.getPrimaryHorizontal(start);//字符左边x坐标
        xAxisRight = layout.getPrimaryHorizontal(end);//字符左边x坐标
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (null == mPaint)
            return;
        if (!isSHowStrikethrough)
            return;
        getAxis();
        // 设置直线位置
        canvas.drawLine(xAxisLeft, this.getHeight()-getPaddingBottom()-5, getRight(),
                -getBottom()+getPaddingTop()+5, mPaint);

    }
}
